package view;

import controller.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.plaf.IconUIResource;
import model.information.Link;

/**
 *
 * @author tobias
 */
public class LinkPanel extends javax.swing.JPanel {
    private Controller ctrl;
    private ArrayList<Link> links;
    private Link currentLink;

    /** Creates new form LiPanel */
    public LinkPanel(Controller ctrl) {
        initComponents();
        this.ctrl = ctrl;
        links = ctrl.getLinks();
        updateList();
    }

    public void updateList() {
        linkList.setListData(links.toArray());
    }

    public void updateFromSave(Object o) {
        linkList.repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        listPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        linkList = new javax.swing.JList();
        sletBtn = new javax.swing.JButton();
        tilføjBtn = new javax.swing.JButton();
        editPanel = new javax.swing.JPanel();
        titelField = new javax.swing.JTextField();
        urlField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        openLinkBtn = new javax.swing.JButton();
        saveBtn = new javax.swing.JButton();

        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(816, 586));

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+3));
        jLabel1.setText("Dine websitelinks");

        listPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Link Liste:"));
        listPanel.setOpaque(false);

        linkList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        linkList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                linkListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(linkList);

        sletBtn.setText("Slet link");
        sletBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sletBtnActionPerformed(evt);
            }
        });

        tilføjBtn.setText("Opret nyt link");
        tilføjBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tilføjBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout listPanelLayout = new org.jdesktop.layout.GroupLayout(listPanel);
        listPanel.setLayout(listPanelLayout);
        listPanelLayout.setHorizontalGroup(
            listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 564, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(sletBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                    .add(tilføjBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
                .addContainerGap())
        );
        listPanelLayout.setVerticalGroup(
            listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(listPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(listPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .add(listPanelLayout.createSequentialGroup()
                        .add(tilføjBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(sletBtn)))
                .addContainerGap())
        );

        editPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Valgte Link:"));
        editPanel.setOpaque(false);

        titelField.setEnabled(false);

        urlField.setEnabled(false);

        jLabel3.setText("Titel");

        jLabel2.setFont(jLabel2.getFont());
        jLabel2.setText("URL");

        openLinkBtn.setText("Åbn Link");
        openLinkBtn.setEnabled(false);
        openLinkBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openLinkBtnActionPerformed(evt);
            }
        });

        saveBtn.setText("Gem");
        saveBtn.setEnabled(false);
        saveBtn.setMaximumSize(new java.awt.Dimension(77, 23));
        saveBtn.setMinimumSize(new java.awt.Dimension(77, 23));
        saveBtn.setPreferredSize(new java.awt.Dimension(77, 23));
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout editPanelLayout = new org.jdesktop.layout.GroupLayout(editPanel);
        editPanel.setLayout(editPanelLayout);
        editPanelLayout.setHorizontalGroup(
            editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(editPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel3)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, titelField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, urlField))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(openLinkBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                    .add(saveBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))
                .addContainerGap())
        );
        editPanelLayout.setVerticalGroup(
            editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(editPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(titelField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3)
                    .add(saveBtn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 10, Short.MAX_VALUE)
                .add(editPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(urlField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(openLinkBtn))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, listPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
                    .add(editPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(listPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(editPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void sletBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sletBtnActionPerformed
        if(linkList.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt noget link der skal slettes, vælg venligst et link og prøv igen.", "Vælg link", JOptionPane.WARNING_MESSAGE);
        } else {
            ctrl.removeLink(currentLink);
        }
}//GEN-LAST:event_sletBtnActionPerformed

    private void tilføjBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tilføjBtnActionPerformed
        Link newLink = ctrl.addLink("Nyt Link", "http://");
        linkList.setSelectedValue(newLink, true);
        titelField.requestFocus();
    }//GEN-LAST:event_tilføjBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        if(titelField.getText().equals("") || urlField.getText().equals("")) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Et eller flere felter er ikke udfyldt korrekt, udfyld venligst både en titel og en URL.", "Manglende oplysninger", JOptionPane.WARNING_MESSAGE);
        } else {
            currentLink.setTitle(titelField.getText());
            currentLink.setUrl(urlField.getText());
        }
}//GEN-LAST:event_saveBtnActionPerformed

    private void linkListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_linkListValueChanged
        if(linkList.getSelectedIndex() != -1) {
            currentLink = (Link)linkList.getSelectedValue();
            titelField.setText(currentLink.getTitle());
            titelField.setEnabled(true);
            urlField.setText(currentLink.getUrl());
            urlField.setEnabled(true);
            saveBtn.setEnabled(true);
            openLinkBtn.setEnabled(true);
        } else {
            titelField.setText("");
            titelField.setEnabled(false);
            urlField.setText("");
            urlField.setEnabled(false);
            saveBtn.setEnabled(false);
            openLinkBtn.setEnabled(false);
        }
    }//GEN-LAST:event_linkListValueChanged

    private void openLinkBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openLinkBtnActionPerformed
        String os = System.getProperty("os.name");
        System.out.println(os);
        if(urlField.getText().equals("") || urlField.getText().equals("http://")){
                JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt et gyldigt link!", "Ingen URL", JOptionPane.INFORMATION_MESSAGE);
            } else {
                try {
                    if(os.toLowerCase().contains("mac os")) {
                        Runtime.getRuntime().exec("open " + urlField.getText());
                    } else if(os.toLowerCase().contains("windows")) {
                        Runtime.getRuntime().exec("cmd.exe /C start " + urlField.getText());
                    } else {
                        JOptionPane.showMessageDialog(ctrl.getFrame(), "Kunne ikke finde din browser!", "Fejl", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (IOException ex) {
                    ctrl.getFrame().warningMessage("Kunne ikke indlæse din browser!", "Fejl");
                }
            }
}//GEN-LAST:event_openLinkBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel editPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList linkList;
    private javax.swing.JPanel listPanel;
    private javax.swing.JButton openLinkBtn;
    private javax.swing.JButton saveBtn;
    private javax.swing.JButton sletBtn;
    private javax.swing.JButton tilføjBtn;
    private javax.swing.JTextField titelField;
    private javax.swing.JTextField urlField;
    // End of variables declaration//GEN-END:variables

}
